package com.qxmly.fm;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.qxmly.fm.adapters.PlayerAdapter;
import com.qxmly.fm.base.BaseActivity;
import com.qxmly.fm.interfaces.IPlayerCallBack;
import com.qxmly.fm.presenters.PlayerPresenter;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.player.service.XmPlayListControl;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Q
 * Date:2021/11/30$
 **/
public class PlayActivity extends BaseActivity implements IPlayerCallBack {

    private static final String TAG = "PlayerActivity";
    private final SimpleDateFormat mMinFormat = new SimpleDateFormat("mm:ss");
    private final SimpleDateFormat mHourFormat = new SimpleDateFormat("hh:mm:ss");
    private ImageView mControlBtn;
    private PlayerPresenter mplayerPresenter;
    private TextView mTotalDuration;
    private TextView mCurrentPosition;
    private SeekBar mDurationBar;
    private int mCurrentProgress = 0;
    private boolean mIsUserTouchProgressBar = false;
    private ImageView mPlayPreBtn;
    private ImageView mPlayNextBtn;
    private TextView mTrackTitleTv;
    private String mTrackTitleText;
    private PlayerAdapter mplayerAdapter;
    private ImageView mPlayingBgIv;
    private ViewPager mTrackPageView;
    private boolean mIsUserSlidePager = false;
    private ImageView mPlayerModeSwitchBtn;

   // private XmPlayListControl.PlayMode mCurrentMode = PLAY_MODEL_LIST;
    //
    private static final Map<XmPlayListControl.PlayMode, XmPlayListControl.PlayMode> sPlayModeRule = new HashMap<>();
   // private String mTrackTitle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);
        mplayerPresenter = PlayerPresenter.getPlayerPresenter();
        mplayerPresenter.registerCallBack(this);
        //透明状态栏
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        initView();
        initEvent();
        mplayerPresenter.play();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mplayerPresenter!=null){
        mplayerPresenter.UnregisterCallBack(this);
        mplayerPresenter= null;}
    }

    private void initEvent() {
        mControlBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mplayerPresenter.isPlaying()){
                    mplayerPresenter.pause();
                } else {
                    mplayerPresenter.play();
                }
            }
        });
        mDurationBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(fromUser){
                    mCurrentProgress=progress;
                }

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                mIsUserTouchProgressBar=true;

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mIsUserTouchProgressBar=true;
                mplayerPresenter.seekTo(mCurrentProgress);
            }
        });
        mPlayPreBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mplayerPresenter!=null){
                    mplayerPresenter.playPre();
                }
            }
        });
        mPlayNextBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mplayerPresenter!=null){
                    mplayerPresenter.playNext();
                }
            }
        });
    }

    private void initView() {
        mControlBtn = findViewById(R.id.play_or_pause_btn);
        mTotalDuration = findViewById(R.id.track_duration);
        mCurrentPosition = findViewById(R.id.current_position);
        mDurationBar = findViewById(R.id.track_seek_bar);
        mPlayPreBtn = findViewById(R.id.play_pre);
        mPlayNextBtn = findViewById(R.id.player_next);
        mTrackTitleTv = findViewById(R.id.track_title);
        if (!TextUtils.isEmpty(mTrackTitleText)) {
            mTrackTitleTv.setText(mTrackTitleText);
        }
        mPlayingBgIv = findViewById(R.id.play_page_bg_iv);

        mTrackPageView = findViewById(R.id.track_pager_view);
        mplayerAdapter =  new PlayerAdapter();
        mTrackPageView.setAdapter(mplayerAdapter);



    }


    @Override
    public void onPlayStart() {
        //开始播放，修改UI层暂停的按钮
        mControlBtn.setImageResource(R.mipmap.stop_normal);
    }

    @Override
    public void onPlayPause() {
        mControlBtn.setImageResource(R.mipmap.play_normal);

    }

    @Override
    public void onPlayStop() {
        mControlBtn.setImageResource(R.mipmap.play_normal);

    }

    @Override
    public void onPlayError(Track track) {

    }

    @Override
    public void onPrePlay(Track track) {

    }

    @Override
    public void onListLoad(List<Track> list) {

    }

    @Override
    public void onPlayModeChange(XmPlayListControl.PlayMode playMode) {

    }

    @Override
    public void onProgressChange(int currentProgress, int total) {
        mDurationBar.setMax(total);
        String totalDuration;
        String currentPosition;
       if(total>1000*60*60){
           totalDuration=mHourFormat.format(total);
           currentPosition=mHourFormat.format(currentProgress);
       } else {
           totalDuration=mMinFormat.format(total);
           currentPosition=mMinFormat.format(currentProgress);
       }
       if(mTotalDuration!=null){
           mTotalDuration.setText(totalDuration);
       }
       if(mCurrentPosition !=null){
           mCurrentPosition.setText(currentPosition);
       }
       //进度条
//        int percent =(int)(currentProgress*1.0f/total*100);
//        LogUtil.d(TAG,"percent->"+percent);
        if(!mIsUserTouchProgressBar) {
            mDurationBar.setProgress(currentProgress);
        }
    }

    @Override
    public void onAdLoading() {

    }

    @Override
    public void onAdFinished() {

    }

    @Override
    public void onTrackUpdate(Track track, int playIndex) {

    }

    @Override
    public void updateListOrder(boolean isReverse) {

    }

    @Override
    public void onTrackTitle(String title) {
            this.mTrackTitleText = title;
        if(mTrackTitleTv!=null){
            mTrackTitleTv.setText(title);
        }
    }
}
